package com.empire.module.member.service.casepersonrelation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.empire.framework.common.pojo.PageResult;
import com.empire.module.member.controller.admin.casepersonrelation.vo.CasePersonRelationPageReqVO;
import com.empire.module.member.controller.admin.casepersonrelation.vo.CasePersonRelationSaveReqVO;
import com.empire.module.member.dal.dataobject.casepersonrelation.CasePersonRelationDO;
import com.empire.module.member.dal.mysql.casepersonrelation.CasePersonRelationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 案件-人员关系 Service 实现类
 *
 * @author 主席大人
 */
@Service
@Validated
public class CasePersonRelationServiceImpl implements CasePersonRelationService {

    @Resource
    private CasePersonRelationMapper casePersonRelationMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer createCasePersonRelation(CasePersonRelationSaveReqVO createReqVO) {
        // 插入新的关系
        List<CasePersonRelationDO> relationList = new ArrayList<>();
        List<Long> personIdList = createReqVO.getPersonIdList();
        for (Long personId : personIdList) {
            CasePersonRelationDO casePersonRelation = new CasePersonRelationDO();
            casePersonRelation.setCaseId(createReqVO.getCaseId());
            casePersonRelation.setPersonId(personId);
            relationList.add(casePersonRelation);
        }
        casePersonRelationMapper.insertBatch(relationList);
        // 返回
        return relationList.size();
    }

    @Override
    public void deleteCasePersonRelation(String caseId, Long personId) {
        // 删除
        LambdaQueryWrapper<CasePersonRelationDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CasePersonRelationDO::getCaseId, caseId);
        queryWrapper.eq(CasePersonRelationDO::getPersonId, personId);
        casePersonRelationMapper.delete(queryWrapper);
    }

    @Override
    public CasePersonRelationDO getCasePersonRelation(Long id) {
        return casePersonRelationMapper.selectById(id);
    }

    @Override
    public Set<Long> getCasePersonRelation(String caseId) {
        LambdaQueryWrapper<CasePersonRelationDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CasePersonRelationDO::getCaseId, caseId);
        List<CasePersonRelationDO> relationDOList = casePersonRelationMapper.selectList(wrapper);
        return relationDOList.stream().map(CasePersonRelationDO::getPersonId).collect(Collectors.toSet());
    }

    @Override
    public PageResult<CasePersonRelationDO> getCasePersonRelationPage(CasePersonRelationPageReqVO pageReqVO) {
        return casePersonRelationMapper.selectPage(pageReqVO);
    }

}